const express = require('express');
const cors = require('cors');
const path = require('path');
const moment = require('moment');
require('dotenv').config();

// 导入路由
const websiteRouter = require('./routes/website');
const booksRouter = require('./routes/books');

const app = express();

// 中间件配置
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 静态文件服务
// 注意：这里不直接暴露epub和cover目录，而是通过API控制访问
app.use('/static', express.static(path.join(__dirname, '../public')));

// 路由配置
app.use('/api/website', websiteRouter);
app.use('/api/books', booksRouter);

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    code: 500,
    message: '服务器内部错误'
  });
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`);
});